<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col v-if="isEmpty(this.$route.query.id)" :span="12">
          <el-form-item :label="$t('基金')" prop="fundId">
            <el-select v-model="form.fundId">
              <el-option
                v-for="(item) in fundOptions"
                :key="item.fundId"
                :label="$t(item.fundName)"
                :value="item.fundId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('出资人名称')" prop="customerId">
            <MainBodySelect
              v-if="currentPage === 'lp'"
              v-model="form.lpId"
              disabled
              parameters="lpInvestor"
            />
            <fund-lp-select v-else v-model="form.customerId" :fund-id="fundId" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('现金流类型')" prop="type">
            <el-select v-model="form.type" :placeholder="$t('现金流类型')">
              <el-option
                v-for="dict in typeOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('业务类型')" prop="businessType">
            <el-select v-model="form.businessType" :placeholder="$t('业务类型')">
              <el-option
                v-for="dict in businessTypeOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('方向')" prop="direction">
            <el-radio-group v-model="form.direction">
              <el-radio
                v-for="dict in directionOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{ $t(dict.dictLabel) }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('发生时间')" prop="operateDate">
            <DatePicker
              v-model="form.operateDate"
              :placeholder="$t('发生时间')"
            />

          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('发生金额') + $t('(元)')" prop="operateAmount">
            <Money v-model="form.operateAmount" :placeholder="$t('发生金额')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('币种')" prop="currency">
            <dictSelect
              v-model="form.currency"
              :clearable="true"
              dictionary="currency"
              @change="changeCurr"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-show="isRate" :label="$t('汇率') " prop="exchangeRate">
            <ExchangeRate v-model="form.exchangeRate" :currency="form.currency" :date="form.operateDate" :placeholder="$t('汇率')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-show="isRate" :label="$t('发生金额') + $t('RMB') + $t('(元)')" prop="operateRmb">
            <ExchangeRateMoney v-model="form.operateRmb" :rate="form.exchangeRate" :money="form.operateAmount" :placeholder="$t('发生金额') + $t('RMB') " />
          </el-form-item>
        </el-col>

        <!--        <el-col :span="12">-->
        <!--          <el-form-item :label="$t('是否参与IRR计算')" prop="whetherIrr">-->
        <!--            <el-radio-group v-model="form.whetherIrr">-->
        <!--              <el-radio-->
        <!--                v-for="dict in whetherIrrOptions"-->
        <!--                :key="dict.dictValue"-->
        <!--                :label="dict.dictValue"-->
        <!--              >{{ $t(dict.dictLabel) }}-->
        <!--              </el-radio>-->
        <!--            </el-radio-group>-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <!--        <el-col :span="12">-->
        <!--          <el-form-item :label="$t('是否参与DPI计算')" prop="dpi">-->
        <!--            <el-radio-group v-model="form.dpi">-->
        <!--              <el-radio-->
        <!--                v-for="dict in whetherIrrOptions"-->
        <!--                :key="dict.dictValue"-->
        <!--                :label="dict.dictValue"-->
        <!--              >{{ $t(dict.dictLabel) }}-->
        <!--              </el-radio>-->
        <!--            </el-radio-group>-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" show-word-limit :maxlength="1000" />
          </el-form-item>
        </el-col>
      </el-row>
      <FileTable :list.sync="form.fileList" data-type="lp_cashflow" :business-id="businessId" :btn-show="!otherBtnHide" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <save-btn has-permi="lpbase:cashflow" />
    </div>
  </div>
</template>

<script>
import { saveLpCashflow, getLpCashflow } from '@/api/fund/lpBase/cashflow/lpCashflow'
import { getSystemCurrency } from '@/api/system/config'
import FundLpSelect from '@/components/Select/fundLpSelect'
import MainBodySelect from '@/components/MainBodySelect'
import { findInvestFundInfo } from '@/api/fund/lpBase/lpBase'
export default {
  components: {
    FundLpSelect,
    MainBodySelect
  },
  data() {
    return {
      otherBtnHide: false,
      // 现金流类型  预计 实际字典
      typeOptions: [], // 方向：流入、流出字典
      directionOptions: [], // 业务类型：基金缴款 基金分配字典
      businessTypeOptions: [], // 是否参与IRR计算字典
      whetherIrrOptions: [], // 表单参数
      // 出资人ID
      lpId: this.$route.query.lpId,
      fundId: this.$route.query.id,
      // 判断当前是在基金里还是在lp里
      currentPage: undefined,
      form: {},
      // 遮罩层
      curr: '',
      isRate: false,
      loading: false,
      fundOptions: [],
      // 表单校验
      rules: {
        fundId: { required: true, message: this.$t('必填信息'), trigger: 'blur' },
        customerId: { required: !this.$route.query.lpId, message: this.$t('必填信息'), trigger: 'blur' },
        type: { required: true, message: this.$t('必填信息'), trigger: 'blur' },
        businessType: { required: true, message: this.$t('必填信息'), trigger: 'blur' },
        operateDate: { required: true, message: this.$t('必填信息'), trigger: 'blur' },
        operateAmount: { required: true, message: this.$t('必填信息'), trigger: 'blur' },
        direction: { required: true, message: this.$t('必填信息'), trigger: 'blur' },
        whetherIrr: { required: true, message: this.$t('必填信息'), trigger: 'blur' },
        currency: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], exchangeRate: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], operateRmb: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], dpi: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ]
      },
      businessId: this.$route.query.lpId
    }
  },
  created() {
    getSystemCurrency().then(res => {
      if (res.code === 200) {
        this.curr = res.data
      } else {
        this.msgError(res.msg)
      }
    })
    // 获取已出资的基金, 从investInfoList 复制过来的.
    findInvestFundInfo({ id: this.$route.query.lpId }).then(response => {
      console.log(response)
      this.fundOptions = response.rows
    })
    this.getDicts('cash_flow_type').then(response => {
      this.typeOptions = response.data
    })
    this.getDicts('cash_flow_direction').then(response => {
      this.directionOptions = response.data
    })
    this.getDicts('lpbase_cashflow_business_type').then(response => {
      this.businessTypeOptions = response.data
    })
    this.getDicts('sys_yes_no').then(response => {
      this.whetherIrrOptions = response.data
    })
    this.$nextTick(() => {
      this.currentPage = (this.$route.query.lpId === undefined ? 'fund' : 'lp')
    })
  },
  methods: {
    // 初始化数据
    init(data) {
      if (data != null && data.id !== undefined) {
        getLpCashflow(data.id).then(response => {
          this.form = response.data
        })
      } else {
        this.isRate = false
        this.reset()
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        lpId: this.$route.query.lpId,
        fundId: undefined,
        type: undefined,
        operateDate: undefined,
        operateAmount: undefined,
        direction: '0',
        businessType: undefined,
        whetherIrr: '0',
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        currency: undefined,
        exchangeRate: undefined,
        operateRmb: undefined,
        dpi: undefined,
        fileList: []
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      this.$parent.$parent.formOpen = false
      // 刷新list页面
      this.$parent.$parent.getList()
    },
    /** 提交按钮 */
    submitForm: function() {
      this.form.lpId = this.lpId
      // 如果币种相同不会用汇率和发生金额rmb 无法提交
      console.log(this.isRate)
      if (!this.isRate) {
        this.form.exchangeRate = 1
        this.form.operateRmb = this.form.operateAmount
      }
      // 判断时不是基金发出的
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          saveLpCashflow(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    },
    // 改变币种触发事件
    changeCurr() {
      if (this.curr !== this.form.currency && this.form.currency) {
        this.isRate = true
      } else {
        this.isRate = false
      }
    }

  }

}
</script>
